home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 December / CHIPNET Aralık 1997.iso / linux / redhat / misc / src / install / scsi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-11  |  5.2 KB  |  254 lines

  1. #include <ctype.h>
  2. #include <errno.h>
  3. #include <fcntl.h>
  4. #include <newt.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8.  
  9. #include "devices.h"
  10. #include "hd.h"
  11. #include "install.h"
  12. #include "log.h"
  13. #include "scsi.h"
  14.  
  15. static int scsiChoicePanel(int * addSCSI);
  16.  
  17. static int scsiChoicePanel(int * addSCSI) {
  18.     newtComponent label, f, answer;
  19.     newtComponent yes, no;
  20.  
  21.     newtOpenWindow(21, 7, 35, 9, "SCSI Configuration");
  22.  
  23.     label = newtLabel(2, 2, "Do you have any SCSI adapters?");
  24.  
  25.     yes = newtButton(5, 5, "Yes");
  26.     no = newtButton(22, 5, "No");
  27.  
  28.     f = newtForm(NULL, NULL, 0);
  29.     newtFormAddComponents(f, label, yes, no, NULL);
  30.     newtFormSetCurrent(f, no);
  31.     
  32.     answer = newtRunForm(f);
  33.     if (answer == f) 
  34.     answer = newtFormGetCurrent(f);
  35.  
  36.     newtFormDestroy(f);
  37.     newtPopWindow();
  38.  
  39.     if (answer == no) 
  40.     *addSCSI = 0;
  41.     else
  42.     *addSCSI = 1;
  43.  
  44.     return 0;
  45. }
  46.  
  47.  
  48. int setupSCSIInterfaces(int forceConfig, struct driversLoaded ** dl) {
  49.     int rc;
  50.     int hasscsi;
  51.  
  52.     if (scsiDeviceAvailable()) return 0;
  53.  
  54.     do {
  55.     if (forceConfig)
  56.         forceConfig = 0;
  57.     else {
  58.         scsiChoicePanel(&hasscsi);
  59.         if (!hasscsi) return 0;
  60.     }
  61.  
  62.         rc = loadDeviceDriver(DRIVER_SCSI, dl);
  63.     if (rc == INST_ERROR) return INST_ERROR;
  64.     } while (rc);
  65.  
  66.     return 0;
  67. }
  68.  
  69. int scsiDeviceAvailable(void) {
  70.     int fd;
  71.     char buf[80];
  72.     int i;
  73.  
  74.     fd = open("/proc/scsi/scsi", O_RDONLY);
  75.     if (fd < 0) {
  76.     logMessage("failed to open /proc/scsi/scsi: %s", strerror(errno));
  77.     return 0;
  78.     }
  79.     
  80.     i = read(fd, buf, sizeof(buf) - 1);
  81.     if (i < 1) {
  82.     logMessage("failed to read /proc/scsi/scsi: %s", strerror(errno));
  83.     return 0;
  84.     }
  85.     close(fd);
  86.     buf[i] = '\0';
  87.  
  88.     logMessage("/proc/scsi/scsi: %s", buf);
  89.  
  90.     if (strstr(buf, "devices: none")) {
  91.     logMessage("no scsi devices are available");
  92.     return 0;
  93.     }
  94.  
  95.     logMessage("scsi devices are available");
  96.     return 1;
  97. }
  98.  
  99. #define SCSISCSI_TOP    0
  100. #define SCSISCSI_HOST     1
  101. #define SCSISCSI_VENDOR 2
  102. #define SCSISCSI_TYPE     3
  103.  
  104. int scsiGetDevices(struct scsiDeviceInfo ** sdiPtr) {
  105.     int fd;
  106.     char buf[16384];
  107.     char linebuf[80];
  108.     char typebuf[10];
  109.     int i, state = SCSISCSI_TOP;
  110.     char * start, * chptr, * next;
  111.     char driveName = 'a';
  112.     char cdromNum = '0';
  113.     char tapeNum = '0';
  114.     int numMatches = 0;
  115.     struct scsiDeviceInfo * sdi;
  116.     int id = 0;
  117.  
  118.     /*sdi = malloc(sizeof(*sdi));*/
  119.     sdi = malloc(16384);
  120.  
  121.     fd = open("/proc/scsi/scsi", O_RDONLY);
  122.     if (fd < 0) {
  123.     logMessage("failed to open /proc/scsi/scsi: %s", strerror(errno));
  124.     return 1;
  125.     }
  126.     
  127.     i = read(fd, buf, sizeof(buf) - 1);
  128.     if (i < 1) {
  129.     logMessage("failed to read /proc/scsi/scsi: %s", strerror(errno));
  130.     return 1;
  131.     }
  132.     close(fd);
  133.     buf[i] = '\0';
  134.  
  135.     start = buf;
  136.     while (*start) {
  137.     chptr = start;
  138.      while (*chptr != '\n') chptr++;
  139.     *chptr = '\0';
  140.     next = chptr + 1;
  141.  
  142.     switch (state) {
  143.       case SCSISCSI_TOP:
  144.         if (strcmp("Attached devices: ", start)) {
  145.         logMessage("unexpected line in /proc/scsi/scsi: %s", start);
  146.         free(sdi);
  147.         return INST_ERROR;
  148.         }
  149.         state = SCSISCSI_HOST;
  150.         break;
  151.  
  152.       case SCSISCSI_HOST:
  153.         if (strncmp("Host: ", start, 6)) {
  154.         logMessage("unexpected line in /proc/scsi/scsi: %s", start);
  155.         free(sdi);
  156.         return INST_ERROR;
  157.         }
  158.  
  159.         start = strstr(start, "Id: ");
  160.         if (!start) {
  161.         logMessage("Id: missing in /proc/scsi/scsi");
  162.         return INST_ERROR;
  163.         }
  164.         start += 4;
  165.  
  166.         id = strtol(start, NULL, 10);
  167.  
  168.         state = SCSISCSI_VENDOR;
  169.         break;
  170.  
  171.       case SCSISCSI_VENDOR:
  172.         if (strncmp("  Vendor: ", start, 10)) {
  173.         logMessage("unexpected line in /proc/scsi/scsi: %s", start);
  174.         free(sdi);
  175.         return INST_ERROR;
  176.         }
  177.  
  178.         start += 10;
  179.         chptr = strstr(start, "Model:");
  180.         if (!chptr) {
  181.         logMessage("Model missing in /proc/scsi/scsi");
  182.         free(sdi);
  183.         return INST_ERROR;
  184.         }
  185.  
  186.         chptr--;
  187.         while (*chptr == ' ') chptr--;
  188.         *(chptr + 1) = '\0';
  189.  
  190.         strcpy(linebuf, start);
  191.         *linebuf = toupper(*linebuf);
  192.         chptr = linebuf + 1;
  193.         while (*chptr) {
  194.         *chptr = tolower(*chptr);
  195.         chptr++;
  196.         }
  197.  
  198.         start = strstr(start + strlen(linebuf) + 1, "Model:");
  199.         start += 7;
  200.         
  201.         chptr = strstr(start, "Rev:");
  202.         if (!chptr) {
  203.         logMessage("Rev missing in /proc/scsi/scsi");
  204.         free(sdi);
  205.         return INST_ERROR;
  206.         }
  207.        
  208.         chptr--;
  209.         while (*chptr == ' ') chptr--;
  210.         *(chptr + 1) = '\0';
  211.  
  212.         strcat(linebuf, " ");
  213.         strcat(linebuf, start);
  214.  
  215.         state = SCSISCSI_TYPE;
  216.  
  217.         break;
  218.  
  219.       case SCSISCSI_TYPE:
  220.         if (strncmp("  Type:", start, 7)) {
  221.         logMessage("unexpected line in /proc/scsi/scsi: %s", start);
  222.         free(sdi);
  223.         return INST_ERROR;
  224.         }
  225.         *typebuf = '\0';
  226.         if (strstr(start, "Direct-Access")) 
  227.         sprintf(typebuf, "sd%c", driveName++);
  228.         else if (strstr(start, "Sequential-Access")) 
  229.         sprintf(typebuf, "st%c", tapeNum++);
  230.         else if (strstr(start, "CD-ROM")) 
  231.         sprintf(typebuf, "scd%c", cdromNum++);
  232.  
  233.         if (*typebuf) {
  234.         /*sdi = realloc(sdi, sizeof(*sdi) * (numMatches + 2));*/
  235.         sdi[numMatches].deviceName = strdup(typebuf);
  236.         sdi[numMatches].info = strdup(linebuf);
  237.         sdi[numMatches].bus = 0;
  238.         sdi[numMatches++].id = id;
  239.         }
  240.  
  241.         state = SCSISCSI_HOST;
  242.     }
  243.  
  244.     start = next;
  245.     }
  246.  
  247.     sdi[numMatches].deviceName = NULL;
  248.     sdi[numMatches].info = NULL;
  249.  
  250.     *sdiPtr = sdi;
  251.  
  252.     return 0;
  253. }
  254.